// pages/follow/index.js
const app=getApp()
const req=require('../../utils/req')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:null,
    category:[],
    current:0,
    currentIndex:0,
    lists:[],
    page:1,
    limit:10,
    empty:false,
    isLoadingAll:false,
    keywords:'',
    crossAxisCount: 2,
    crossAxisGap: 4,
    mainAxisGap: 4,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    /*
    if(options.id){
      this.setData({
        id:options.id
      })

    }
    this.getCategory()
    */
   
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log('onShow')
    this.setData({
      lists:[],
      page:1,
      limit:10,
      empty:false,
      isLoadingAll:false,
    })
    var userLogin=wx.getStorageSync('userLogin')
    if(userLogin && userLogin.mobile){
      this.getList()
    }else{
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      lists:[],
      page:1,
      limit:10,
      empty:false,
      isLoadingAll:false,
    })
    this.getList()

    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(!this.data.isLoadingAll){
        this.data.page++
        this.getList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getCategory:function(){
    var that=this
    var id=this.data.id
    if(!id){
      id=1
    }
    req.request('/ht/user/homeType',{},'POST').then(res=>{
      var datas=res.data
      var len=datas.length
      var current=datas.findIndex(item=>item.id==id)
     // datas=datas.concat(datas)

      that.setData({
        category:datas,
        current:current,
      })
      console.log(current%5)
      if(current>=4){
        console.log(1)
          var currentIndex=null
          if(current-1>5){
            currentIndex=5
          }else{
            currentIndex=current-1
          }
          that.setData({
            currentIndex:currentIndex
          })
      }
      that.getList()
     
    })
  },

  tab:function(e){
    var that=this
    var category=this.data.category
    var len=category.length
    var index=e.currentTarget.dataset.index
    var currentIndex=this.data.currentIndex
    console.log(index,currentIndex)
    if(index>currentIndex && index-currentIndex>=4 && len-index>1){
      console.log(1)
      this.setData({
        currentIndex:currentIndex+1
      })
    }else if(index<=currentIndex && index){
      console.log(2)
      this.setData({
        currentIndex:currentIndex-1
      })
    }
    that.setData({
      current:index,
      lists:[],
      page:1,
      empty:false,
      isLoadingAll:false,
      keywords:''
    })
    this.getList()
    
  },
  getList:function(){
    var that=this
    var category=this.data.category
    var current=this.data.current
    var userInfo=wx.getStorageSync('userInfo')
    var data={}
    data.id=userInfo.userId
    data.page=this.data.page
    data.limit=this.data.limit
    //data.storeTypeId=category[current].id
    //data.type=0
    if(this.data.keywords){
      data.keywords=this.data.keywords
    }
    app.getList('/ht/user/userCollectList',data,'POST',that)
  },

  toPackage:function(e){
    var item=e.currentTarget.dataset.item
    wx.navigateTo({
      url: '/pages/timeCar/index?data='+item.id+'&storeId='+item.storeId+'&storeName='+item.storeName,
    })
  },
  toStore:function(e){
    var id=e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/storeDetail/index?data='+id
    })
  },
  submit:function(e){
    console.log(e)
    console.log('keywords',e.detail.value.keywords,'--',e.detail.value)
    var keywords=e.detail.value.keywords || e.detail.value
    console.log(keywords)
   if(keywords){
    this.setData({
      keywords:keywords,
      lists:[],
      page:1,
      empty:false,
      isLoadingAll:false,
    })
    this.getList()
   }else{
      wx.showToast({
        title: '请输入关键词',
        icon:'none',
        duration:1500
      })
   }
  },
})